#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: send_beanstalk_compare.py
@time: 2018/1/21 13:22
"""

import sys

sys.path.append('..')
sys.path.append('../..')

from common import util
from common.mongo import MongDb
from common.table_manage import TableManage
from config.beanstalk_conf import BEANSTALK_CONF
from config.mongo_conf import MONGO_DB_SOURCE
from logger import Logger

IMPORT_FILE_DICT = {
    'hunan': 'hunan.txt',
    'gansu': 'gansu.txt',
    'jiangsu': 'jiangsu.txt',
    'shandong': 'shandong.txt',
}
# 日志模块
global_log = Logger('reset_crawl_status.log').get_logger()


class SendBeanstalk(object):
    def __init__(self, beanstalk_conf, tube, _log):
        self.log = _log
        #
        # # 初始化消息队列
        # self.beanstalk_thread = BeanstalkHandler(beanstalk_conf=beanstalk_conf, log=_log)
        # self.beanstalk_thread.start()

        # 初始化mongodb
        self.source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                                MONGO_DB_SOURCE['username'],
                                MONGO_DB_SOURCE['password'], log=_log)

        self.tube = tube

    def start(self):

        for province, file_name in IMPORT_FILE_DICT.iteritems():
            # 种子表名称
            seed_table = TableManage.get_seed_table(province)

            with open(file_name) as p_file:
                for line in p_file:
                    company = line.strip('\n').strip('\r')
                    item = self.source_db.find_one(seed_table, {'_id': company})
                    if item is None:
                        self.source_db.insert_batch_data(seed_table, [{
                            '_id': company,
                            '_in_time': util.get_now_time(),
                            'search_status': 0,
                            'crawl_status': 0
                        }])

                        continue

                    crawl_status = item.get('crawl_status')
                    if crawl_status == 1:
                        continue

                    search_status = item.get('search_status')
                    if search_status == 2:
                        self.source_db.insert_batch_data(seed_table, [{
                            '_id': company,
                            '_in_time': util.get_now_time(),
                            'search_status': 0,
                            'crawl_status': 0
                        }])
                        continue


def main():
    SendBeanstalk(BEANSTALK_CONF, tube='test_list_download', _log=global_log).start()


if __name__ == '__main__':
    main()
